package org.example.web;

import org.example.model.Admin;
import org.example.model.Menu;
import org.example.model.Result;
import org.example.service.AdminLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/adminApi/loginCtl")
public class AdminLoginController {

    @Autowired
    AdminLoginService loginService;
    @RequestMapping(path = "/login")
    public Result login(@RequestBody Admin admin){
        //加密密码
        admin.setPassword(DigestUtils.md5DigestAsHex(admin.getPassword().getBytes()));

        System.out.println(admin.getPassword());
        Admin admin1 = loginService.login(admin);
        if(admin1 != null){
            return new Result(200,"success",admin1);
        }else {
            return new Result(400,"fail","用户名或密码错误");
        }
    }
    @RequestMapping(path = "/menus",method = RequestMethod.GET)
    public Result menus(@RequestHeader("adminToken")String adminToken){
        List<Menu> menus = loginService.getMenus(adminToken);
        return new Result(200,"success",menus);

    }

}
